package com.butte.mound.frame.entity;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.ArrayList;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotNull;
import java.util.List;

@ApiModel(value = "分页VO")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageVO<T> {

    @ApiModelProperty(value = "分页数据")
    private List<T> records;

    @ApiModelProperty(value = "总数")
    private long total;

    @ApiModelProperty(value = "当前页")
    private long current;

    @ApiModelProperty(value = "分页大小")
    private long size;

    public static <OUT, T> PageVO<OUT> convert(@NotNull IPage<T> page, Class<OUT> target) {
        List<T> sourceList = page.getRecords();
        List<OUT> targetList = new ArrayList<>(sourceList.size());
        try {
            for(Object source : sourceList){
                OUT targetObject = target.newInstance();
                BeanUtil.copyProperties(source, targetObject);
                targetList.add(targetObject);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new PageVO<>(targetList,page.getTotal(), page.getCurrent(), page.getSize());
    }

    public static <T> PageVO<T> convert(@NotNull IPage<T> page) {
        return new PageVO<>(page.getRecords(),page.getTotal(), page.getCurrent(), page.getSize());
    }
}